/**
 * Created by haocongshun on 2016/9/9.
 */
$(function () {
    init();
    bindreturn();
});
function bindreturn() {
    $("#feedback").click(function () {
        var url="/WorkManager/message/answer.wm"
        var data={};
        data.flag=location.search.split("&")[0].substring(1);
        data.work_id=location.search.split("=")[1];
        if($.trim($("#rediv textarea").val())==""||$.trim($("#rediv textarea").val())==null){
            alert("回复内容不能为空！");
            return;
        }
        data.remsg=$.trim($("#rediv textarea").val());
        $.post(url,data,function (result) {
            if(result.msg){
                init();
            }else {
                alert("回复失败！");
            }
        },"json");
    });
    $("#cancel").click(function () {
        history.go(-1);
    });
}
function init() {
    var url="/WorkManager/message/talk.wm";
    var dataInit = {};
    dataInit.id=location.search.split("=")[1];
    var toflag=location.search.split("&")[0].substring(1);
    $.post(url,dataInit,function (result) {
        var MSG = [];
        var teacher=result.talk.teacher.teacher_name;
        var student=result.talk.student.student_name;
        for(var i = 0;i < result.talk.feedback.length;i++){
            var ceil = {};
            var date = (date = result.talk.feedback[i].feedback_time).substring(0,date.length-2)
            var day = date.split(" ")[0];
            var time = (time = date.split(" ")[1]);
            ceil.day = day;
            ceil.time = time;
            ceil.date = new Date(date.replace(/-/g,"/"));
            ceil.flag="feedback";
            ceil.content = result.talk.feedback[i].feedback_content;
            MSG.push(ceil);
        }
        for(var i = 0;i < result.talk.evaluate.length;i++){
            var ceil = {};
            var date = (date = result.talk.evaluate[i].submit_time).substring(0,date.length-2)
            var day = date.split(" ")[0];
            var time = (time = date.split(" ")[1]);
            ceil.day = day;
            ceil.time = time;
            ceil.date = new Date(date.replace(/-/g,"/"));
            ceil.flag="evaluate";
            ceil.content = result.talk.evaluate[i].evaluate;
            MSG.push(ceil);
        }
        MSG.sort(function (a,b) {
            return b.date - a.date;
        })
        var newHtml = "";
        for(var i = 0;i < MSG.length;i ++){
            if(MSG[i].flag=="feedback"){
              newHtml+="<dl class='vme m1'>" +
                  " <dt><a>"+(toflag=="stu"?" 我":student)+"</a>" +
                  "<span>"+MSG[i].day+"&nbsp;&nbsp;&nbsp;&nbsp;"+MSG[i].time+"</span>" +
                  "</dt>" +
                  " <dd>"+MSG[i].content+"</dd>" +
                  " </dl>";
            }
            if(MSG[i].flag=="evaluate"){
                newHtml+="<dl class='vhe m1'>" +
                    " <dt><a>"+(toflag=="tea"?"我":teacher)+"</a>" +
                    "<span>"+MSG[i].day+"&nbsp;&nbsp;&nbsp;&nbsp;"+MSG[i].time+"</span>" +
                    "</dt>" +
                    " <dd>"+MSG[i].content+"</dd>" +
                    " </dl>";
            }
        }
        $("#rediv h4 span").html(toflag=="stu"?teacher+"老师":student+"同学");
        $("#talk").html(newHtml);

    },"json");
}